package com.ihrm.system.controller;


import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.PageResult;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.domain.system.User;
import com.ihrm.system.service.UserService;
import com.ihrm.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtils jwtUtils;

    //保存用户
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public Result add(@RequestBody User user) throws Exception{
        user.setCompanyId(parseCompanyId());
        user.setCompanyName(parseCompanyName());
        userService.save(user);
        return Result.SUCCESS();
    }

    //更新用户
    @RequestMapping(value = "/user/{id}",method = RequestMethod.PUT)
    public Result update(@PathVariable(name = "id")String id,@RequestBody User user)throws Exception{
        userService.update(user);
        return Result.SUCCESS();
    }

    //删除用户
    @RequestMapping(value = "/user/{id}" ,method = RequestMethod.DELETE)
    public Result delete(@PathVariable(name = "id")String id)throws Exception{
        userService.delete(id);
        return Result.SUCCESS();

    }

    //根据Id查询用户
    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public Result findById(@PathVariable(name = "id")String id)throws Exception{
        User user = userService.findById(id);
        return new Result(ResultCode.SUCCESS,user);
    }

    //分页查询用户
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public Result findByPage(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "2") int pagesize, @RequestParam Map<String,Object>map)throws Exception{
        map.put("companyId",parseCompanyId());
        Page<User> searchPage = userService.findSearch(map,page,pagesize);
        PageResult<User> pr= new PageResult(searchPage.getTotalElements(),searchPage.getContent());
        return new Result(ResultCode.SUCCESS,pr);
    }
    /**
     * 分配角色
     */
    @RequestMapping(value = "/user/assignRoles", method = RequestMethod.PUT)
    public Result assignRoles(@RequestBody Map<String,Object> map) {
        //1.获取被分配的用户id
        String userId = (String) map.get("id");
        //2.获取到角色的id列表
        List<String> roleIds = (List<String>) map.get("roleIds");
        //3.调用service完成角色分配
        userService.assignRoles(userId,roleIds);
        return new Result(ResultCode.SUCCESS);
    }
    /**
     * 用户登录
     * 1.通过service根据mobile查询用户
     * 2.比较password
     * 3.生成jwt信息
     *
     */
    @RequestMapping(value="/login",method = RequestMethod.POST)
    public Result login(@RequestBody Map<String,String> loginMap) {
        String mobile = loginMap.get("mobile");
        String password = loginMap.get("password");
        User user = userService.findByMobileAndPassword(mobile,password);
        //登录失败
        if(user == null || !user.getPassword().equals(password)) {
            return new Result(ResultCode.UNAUTHENTICATED);
        }else {
            //登录成功
            Map<String,String> map = new HashMap<>();
            map.put("companyId",user.getCompanyId());
            map.put("companyName",user.getCompanyName());
            String token = jwtUtils.createJwt(user.getId(), user.getUsername(),map);
            return new Result(ResultCode.SUCCESS,token);
        }
    }

}

